import { Controller, Get, Post, Body, Query, Put, Param, Delete } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
//import { PostModel } from './post.model';
import { isNotEmpty, IsNotEmpty } from 'class-validator'
import { InjectModel } from 'nestjs-typegoose';
import {Post as PostSchema} from './post.model'
import { ModelType } from '@typegoose/typegoose/lib/types';

class CreatePostDto{
	@ApiProperty({description:'帖子标题',example:'title11111'})
	@IsNotEmpty({message:'不能为🈳️'})
	title: string
	@ApiProperty({description:'帖子内容',example:'content11111'})
	content: string
}

@Controller('posts')
@ApiTags('帖子')
export class PostsController {
//依赖注入
	constructor(
		@InjectModel(PostSchema) private readonly postModel:ModelType<PostSchema>
	){}

    @Get()
    @ApiOperation({summary:'显示帖子列表'})
    async index(){
        return await this.postModel.find()
    }
    @Post()
    @ApiOperation({summary:'创建帖子'})
    async create(@Body() createPostDto: CreatePostDto){
			await this.postModel.create(createPostDto)
      return {
				success:true
			}
    }
		@Get(':id')
		@ApiOperation({summary:'帖子详情'})
    async detail(@Param('id') id:string){
			return await this.postModel.findById(id)
		}
		@Put(':id')
		@ApiOperation({summary:'编辑帖子'})
		async update(@Param('id') id:string,@Body() updatePostDot:CreatePostDto){
			await this.postModel.findByIdAndUpdate(id,updatePostDot)
			return {
				success:true
			}
		}
		@Delete(':id')
		@ApiOperation({summary:'删除帖子'})
		async remove(@Param('id') id:string){
			await this.postModel.findByIdAndDelete(id)
			return {
				success:true
			}
		}
}
